type TFn<T> = (arr: T[]) => T[];

export function measureSort(fn: TFn<number>, length = 100000) {
  const arr = Array.from({ length }, () => {
    return Math.floor(Math.random() * length);
  });
  const start = performance.now();
  fn(arr);
  const endTime = performance.now();

  const timeElapsed = (endTime - start).toFixed(2);
  console.log(
    `使用 ${fn.name} 算法 排序 ${length} 个元素 消耗时间为 ${timeElapsed} 毫秒`
  );
}

function isSort<T>(arr: T[]) {
  const n = arr.length;
  for (let i = 0; i < n - 1; i++) {
    if (arr[i] > arr[i + 1]) {
      return false;
    }
  }

  return true;
}

export function testSort(fn: TFn<number>) {
  const arr = Array.from({ length: 10 }, () => {
    return Math.floor(Math.random() * 100);
  });

  console.log("原数组", arr);
  fn(arr);
  console.log("排序数组", arr);
  console.log("是否排序正确", isSort<number>(arr));
}

export function swap(arr: number[], i: number, j: number) {
  const temp = arr[i];
  arr[i] = arr[j];
  arr[j] = temp;
}
